package com.glsc.ngateway.platform.service.it;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.*;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author weizt
 */
@Service
public class DomianOpearationService {

    Logger logger = LoggerFactory.getLogger(DomianOpearationService.class);

    @Autowired
    private ItDomainRepo itDomainRepo;

    @Autowired
    private ItDomainManageRepo itDomainManageRepo;

    @Autowired
    private ItOfficeDomainRepo itOfficeDomainRepo;

    @Autowired
    private WideIpNetworkMapRepo wideIpNetworkMapRepo;

    @Autowired
    private ItDomainPortRepo itDomainPortRepo;

    @Autowired
    private ItDomainLogRepo itDomainLogRepo;

    @Autowired
    private ItDomainManageWithChildRepo itDomainManageWithChildRepo;

    public Page<ItDomain> searchByKey(String key, Integer pageSize, Integer pageNum) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<ItDomain> list = itDomainRepo.searchByKey(pageable, key);
        return list;
    }

    public void addOrUpdate(ItDomain bean) {
        logger.info("addOrUpdate-itdomian"+bean);
        bean.setOaAccount(bean.getAdministrator());
        itDomainRepo.save(bean);
    }

    public void deleteById(String id) {
        logger.info("delete-itdomian"+id);
        Optional<ItDomain> dev = itDomainRepo.findById(Integer.valueOf(id));
        if (dev.isPresent()) {
            dev.get().setIsDelete(DictConstant.DELETED);
            itDomainRepo.save(dev.get());
        }
    }

    public List<ItDomain> domainListAll() {
        return itDomainRepo.findAllByIsDelete(DictConstant.NO_DELETED);
    }

    public Page<ItDomainManageWithChild> searchDomainManageListByKey(String key, Integer pageSize, Integer pageNum) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<ItDomainManageWithChild> list = itDomainManageWithChildRepo.searchByKey(pageable, key);
        if(list != null && CollectionUtil.isNotEmpty(list.getContent())){
            List<ItDomain> domainList = this.domainListAll();
            Map<Integer,String> domainMap = domainList.stream().collect(Collectors.toMap(ItDomain::getId,ItDomain::getDomain, (key1, key2) -> key2));
            for (ItDomainManageWithChild manage:list) {
                manage.setDomain(domainMap.get(manage.getDomainId()));
                //服务商为无锡电信且类型为CNAME则显示子集，否则设置为空
                if(!DictConstant.DOMAIN_TYPE_CNAME.equals(manage.getType()) ||  !DictConstant.DOMAIN_NAME_SERVER_无锡电信.equals(manage.getNameServer())){
                    manage.setChildren(new HashSet<>());
                } else {
                    Set<ItDomainWideIpNetworkMap> sets = manage.getChildren();
                    if (CollectionUtil.isNotEmpty(sets)){
                        for (ItDomainWideIpNetworkMap wideIp:sets) {
                            if (StrUtil.isNotEmpty(wideIp.getMembers())){
                                wideIp.setChildren(Arrays.stream(wideIp.getMembers().split(",")).map(str->new ChildBean(str)).collect(Collectors.toList()));
                                wideIp.setMembers(null);
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    public void addOrUpdate(ItDomainManage bean) {
        logger.info("addOrUpdate-ItDomainManage"+bean);
        bean.setOaAccount(bean.getAdministrator());
        itDomainManageRepo.save(bean);
    }

    public void deleteDomainManageListDeleteById(String id) {
        logger.info("delete-ItDomainManage"+id);
        Optional<ItDomainManage> dev = itDomainManageRepo.findById(Integer.valueOf(id));
        if (dev.isPresent()) {
            dev.get().setIsDelete(DictConstant.DELETED);
            itDomainManageRepo.save(dev.get());
        }
    }

    public Page<ItDomainOffice> searchOfficeDomainListByKey(String key, Integer pageSize, Integer pageNo) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        Page<ItDomainOffice> list = itOfficeDomainRepo.searchByKey(pageable, key);
        return list;
    }

    public void addOrUpdate(ItDomainOffice bean) {
        logger.info("addOrUpdate-ItOfficeDomain"+bean);
        bean.setOaAccount(bean.getAdministrator());
        itOfficeDomainRepo.save(bean);
    }

    public void officeDomainListDeleteById(String id) {
        logger.info("delete-ItOfficeDomain"+id);
        Optional<ItDomainOffice> dev = itOfficeDomainRepo.findById(Integer.valueOf(id));
        if (dev.isPresent()) {
            dev.get().setIsDelete(DictConstant.DELETED);
            itOfficeDomainRepo.save(dev.get());
        }
    }

    public void truncateWideIpNetworkMap() {
        logger.info("清空WideIpMap");
        wideIpNetworkMapRepo.truncateTable();
    }

    public void saveWideIpNetworkMap(ItDomainWideIpNetworkMap itDomainWideIpNetworkMap) {
        wideIpNetworkMapRepo.save(itDomainWideIpNetworkMap);
    }

    public List<ItDomainWideIpNetworkMap> findAllList() {
        List<ItDomainWideIpNetworkMap> list = wideIpNetworkMapRepo.findAll();
        return list;
    }

    public List<ItDomainPort> domainPortListAll() {
        List<ItDomainPort> list = itDomainPortRepo.findAll();
        return list;
    }

    public Page<ItDomainPort> searchDomainPortListByKey(String key, Integer pageSize, Integer pageNo) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        Page<ItDomainPort> list = itDomainPortRepo.searchByKey(pageable, key);
        return list;
    }

    public void addOrUpdate(ItDomainPort bean) {
        logger.info("addOrUpdate-ItDomainPort"+bean);
        itDomainPortRepo.save(bean);
    }

    public void domainPortListDeleteById(String id) {
        logger.info("delete-ItDomainPort"+id);
        Optional<ItDomainPort> dev = itDomainPortRepo.findById(Integer.valueOf(id));
        if (dev.isPresent()) {
            dev.get().setIsDelete(DictConstant.DELETED);
            itDomainPortRepo.save(dev.get());
        }
    }



    public List<ItDomainLog> domainLogListAll() {
        List<ItDomainLog> list = itDomainLogRepo.findAll();
        return list;
    }

    public Page<ItDomainLog> searchDomainLogListByKey(String key, Integer pageSize, Integer pageNo) {
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
        Page<ItDomainLog> list = itDomainLogRepo.searchByKey(pageable, key);
        return list;
    }

    public void addOrUpdate(ItDomainLog bean) {
        logger.info("addOrUpdate-ItDomainLog"+bean);
        itDomainLogRepo.save(bean);
    }

    public void domainLogListDeleteById(String id) {
        logger.info("delete-ItDomainLog"+id);
        Optional<ItDomainLog> dev = itDomainLogRepo.findById(Integer.valueOf(id));
        if (dev.isPresent()) {
            dev.get().setIsDelete(DictConstant.DELETED);
            itDomainLogRepo.save(dev.get());
        }
    }
    
}
